<?PHP	//header
include('config/config.php');
include('php/header.php');
require "php/functions.php";
?>        

<?PHP	//Connect to database
	$conn = mysql_connect($cfg["connection"]["host"], $cfg["connection"]["userName"], $cfg["connection"]["password"]) 
		or die ('Error Cannot Connect to MySQL');
	@mysql_select_db($cfg["connection"]["database"]) or die( "Unable to select database");
	session_start();
	if (isset($_SESSION['checkbox_Inventory']) && is_array($_SESSION['checkbox_Inventory'])) {
//			println($_POST['checkbox_Inventory'][0]);
		$column_name = $cfg["tbl_inventory"]["barcode"];
		$column_targetVal = $_SESSION['checkbox_Inventory'][0]; //only consider 1st checkbox
		$tbl_name = $cfg["tbl"]["inventory"];
		$sql = "SELECT * FROM $tbl_name WHERE $column_name = $column_targetVal";
		$result = mysql_query($sql) or die (mysql_error());
		$row = mysql_fetch_array($result);
		$itemname = $row[$cfg['tbl_inventory']['product_name']];
		$barcode = $row[$cfg['tbl_inventory']['barcode']];
		$category = $row[$cfg['tbl_inventory']['category']];
		$manufacturer = $row[$cfg['tbl_inventory']['manufacturer']];
		$price = $row[$cfg['tbl_inventory']['price']];
		$local_stock = $row[$cfg['tbl_inventory']['local_stock']];
		$minimum_stock = $row[$cfg['tbl_inventory']['minimum_stock']];
		$bundle_unit = $row[$cfg['tbl_inventory']['bundle_unit']];
	}
?>



<h2>Product Update</h2> 
		<?PHP
			if(isset($_POST['itemname']))
			{
				$name = $_POST['itemname'];
				$category = $_POST['cat'];
				$manuf = $_POST['man'];
				$barcode = $_POST['bar'];
				$currentStock = $_POST['cstock'];
				$costprice = $_POST['price'];
				$minStock = $_POST['mstock'];
				$bundleunit = $_POST['bundleunit'];
				
				$tbl_name = $cfg["tbl"]["inventory"];
				$query = "INSERT INTO $tbl_name (itemId, itemName, price, category, manufacturer, local_stock, minimum_stock, bundle_unit) 
						VALUES('".$barcode."', '".$name."', '".$costprice."', '".$category."', '".$manuf."', '".$currentStock."', '".$minStock."', '".$bundleunit."')
						ON DUPLICATE KEY UPDATE itemName = '".$name."', price = '".$costprice."', category = '".$category."', manufacturer = '".$manuf."', local_stock = '".$currentStock."', minimum_stock = '".$minStock."', bundle_unit = '".$bundleunit."';";
				$result = mysql_query($query);

				if($result)	echo("<div class=\"valid_box\">Product Successfully Updated to Inventory</div>");
				else echo("<div class=\"error_box\">Update Product Failed</div>");
			}	
		?>
         <div class="form">
         <form action="<?PHP $_PHP_SELF_?>" method="post" id="ProductAdding" class="niceform">
                <fieldset>
                    <dl>
                        <dt><label for="email">Product Name:</label></dt>
                        <dd><?PHP echo "<input type=\"text\" name=\"itemname\" size=\"54\" value=\"$itemname\"/>" ?></dd>
                    </dl>
                    <dl>
                        <dt><label for="password">Category:</label></dt>
                        <dd><?PHP echo "<input type=\"text\" name=\"cat\" size=\"54\" value=\"$category\"/>" ?></dd>
                    </dl>
                    <dl>
                        <dt><label for="password">Manufacturer:</label></dt>
                        <dd><?PHP echo "<input type=\"text\" name=\"man\" size=\"54\" value=\"$manufacturer\"/>" ?></dd>
                    </dl>
                    <dl>
                        <dt><label for="password">Barcode:</label></dt>
                        <dd><?PHP echo "<input type=\"text\" name=\"bar\" size=\"56\" value=\"$barcode\" style = \"font-style: italic\" placeholder=\"$barcode\" readonly />" ?></dd>
                    </dl>
					<dl>
                        <dt><label for="password">Cost Price:</label></dt>
                        <dd><?PHP echo "<input type=\"text\" name=\"price\" size=\"54\" value=\"$price\"/>" ?></dd>
                    </dl>
					<dl>
                        <dt><label for="password">Current Stock:</label></dt>
                        <dd><?PHP echo "<input type=\"text\" name=\"cstock\" size=\"54\" value=\"$local_stock\"/>" ?></dd>
                    </dl>
					<dl>
                        <dt><label for="password">Minimum Stock:</label></dt>
                        <dd><?PHP echo "<input type=\"text\" name=\"mstock\" size=\"54\" value=\"$minimum_stock\"/>" ?></dd>
                    </dl>
					<dl>
                        <dt><label for="password">Bundle Unit:</label></dt>
                        <dd><?PHP echo "<input type=\"text\" name=\"bundleunit\" size=\"54\" value=\"$bundle_unit\"/>" ?></dd>
                    </dl>
                    <dl>
                        <dt><label></label></dt>
                        <dd>
                            <input type="checkbox" name="interests[]" id="" value="" checked /><label class="check_label">I agree to the <a href="#">terms &amp; conditions</a></label>
                        </dd>
                    </dl>
                    
                     <dl class="submit">
                    <input type="submit" name="submit" id="submit" value="Submit" />
                     </dl>  
                </fieldset>       
         </form>
         </div>  

<div class="pagination">
<?PHP	//footer
include('php/footer.php');
?>
<?PHP
		// if (isset($_POST['checkbox_Inventory']) && is_array($_POST['checkbox_Inventory']) ){
			// foreach($_POST['checkbox_Inventory'] as $inBarcode)
				// echo "<tr>".$inBarcode."</tr>";
		// }
?>